\section{Activating the Real-time Kernel}
This section describes how to setup an own kernel with real-time
capabilities and how to setup the Ubuntu real-time kernel. Furthermore it
compares both systems to a standard kernel with regards to response times,
performance and stress handling.

\subsection{Realtime Capabilities of the Vanilla Kernel}
The Vanilla kernel is a minimal kernel without any distribution dependent
features. Linux distributions like Ubuntu, Debian, SuSe or RedHat build their
kernel with several patches. Those extend functionalities based on special
needs of different distributions.

There are features in the Vanilla kernel that can be used to ease the
handling of the Vanilla kernel as a real-time kernel. Examples are:

\paperDescription{CONFIG\_PREEMPT\_VOLUNTARY}{This kernel patch checks
options that mostly cause higher latency on systems. For instance there is a
possibility to gain voluntary CPU time to processes with higher priorities,
which is the first step to successful preemption.}

\paperDescription{CONFIG\_PREEMPT}{This kernel patch adds the option, to
gain access to processes outside spinlock-protected areas on a
non-voluntary base. This leads to better latency and system wide
performance.}

These changes help to get a better latency for the kernel. 
To become a real-time kernel it is necessary to be fully preemptable. This can
be achieved by, for example, an external kernel patch, called
\emph{CONFIG\_PREEMPT\_RT}.

\subsection{Making the kernel fully preemptable}
In order to get a fully preemptable kernel it is necessary to patch the kernel.
This kernel patch changes the following options:
\begin{itemize}
  \item In-Kernel locking-primitives locked by spinlocks get preemptable.
  \item Critical sections locked by spinlocks\_t, rwlock\_t, \dots get
  preemptable.
  \item Priority inheritance is added to in-kernel spinlocks and semaphores.
  \item Converting interrupt handlers into preemptable kernel threads.
  \item Userspace POSIX timer with high resolution due to reorganization of
  Linux timer API infrastructure.
\end{itemize}

\subsection{Building an own kernel}
\label{subsec:OwnKern}
In the following listings all steps from downloading up to installing an own,
fully preemptable kernel are described.
\begin{description}[\IEEEsetlabelwidth{Listing X}]
  \item[Listing \ref{lst:kernelDownload}] This listing describes the
  download and unpacking procedure of the kernel. This example works with the
  latest stable kernel version 2.6.32.1 (This was the current version during
  the development of this paper).
  \item[Listing \ref{lst:kernelPatching}] This listing describes how to patch
  a kernel with the \emph{CONFIG\_PREEMPT\_RT} kernelpatch.
  \item[Listing \ref{lst:kernelConfiguring}] This listing describes how to
  activate the necessary changes in the kernel config file. It is essential
  to change the following options:
\begin{itemize}
    \item CONFIG\_PREEMPT\_RT to activate the kernel patch.
    \item CONFIG\_HIGH\_RES\_TIMERS needs to be activated to have the high
    resolution timer in the real-time system.
    \item CONFIG\_ACPI and CONFIG\_APM needs to be disabled in order to
    deactivated features like \emph{CPU frequency scaling} or \emph{hibernation}
    and other power saving options.
\end{itemize}
  \item[Listing \ref{lst:kernelCompiling}] This listing describes how to
  compile the preconfigured kernel. After compilation, the modules have to be
   compiled and installed as well. Moreover it is necessary to copy the
  kernelimage to the boot directory. This is done automatically by the
  \emph{make install} command. 
\end{description}

\begin{lstlisting}[label=lst:kernelDownload, float=!t, language=bash,
caption=Kernel download and unpacking] 
# Getting the newest vanilla kernel from kernel.org
> wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.1.tar.bz2 
# Make a directory for your new kernel and unpack it into this directory 
> mkdir rtKernel 
> mv linux-2.6.32.1.tar.bz2 rtKernel/
> cd rtKernel/
> tar jxvf linux-2.6.32.1.tar.bz2
\end{lstlisting}

\begin{lstlisting}[label=lst:kernelPatching, float=ht, language=bash,
caption=Kernel patching]
# First, download the CONFIG_PREEMPT_RT Patch  
> wget
http://www.kernel.org/pub/linux/kernel/projects/rt/patch-2.6.23.1-rt11.bz2 
> cd linux-2.6.32.1/ 
# Then patch it ! 
> bzcat ../patch-2.6.23.1-rt11.bz2 | patch -p1
\end{lstlisting}

\begin{lstlisting}[label=lst:kernelConfiguring, float=!t, language=bash,
caption=Kernel configuring]
# Configuring necessary changes in the .config file:
> vim rtKernel/linux-2.6.32.1/.config
## Enable CONFIG_PREEMPT_RT
CONFIG_PREEMPT_RT=y
## High Resolution Timer
CONFIG_HIGH_RES_TIMERS=y
## deactivate ACPI / APM modules
CONFIG_ACPI=n
CONFIG_APM=n
# Save configuration and exit
\end{lstlisting}

\begin{lstlisting}[label=lst:kernelCompiling, float=!t, language=bash,
caption=Kernel compiling]
# Compile the patched kernel and generate the kernelimage
> make
# Compile modules
> make modules_install
# Install kernelimage and modules
> make install
\end{lstlisting}

\subsection{Optional Changes}
In addition to the essential changes in the kernel, there are several
optional changes that help improving the kernel. Furthermore it is necessary to
know with which hardware the kernel has to deal with. A Sony Vaio
Laptop VGN-TZ11XN with the following hardware specification is used for the
tests: 
\begin{description}[\IEEEsetlabelwidth{RAM}]
  \item[CPU] Intel Core 2 Duo (1.06 GHz, 2MB Cache, 533MHz FSB)
  \item[RAM] 1x2GB DDR2-SDRAM
  \item[HDD] Ultra ATA/100, circa 30MB/s tested buffered disk reads with
  \emph{hdparm}.
\end{description}

Most of the necessary information can be gathered by the following commands:

\paperDescription{cat /proc/cpuinfo}{This command shows the content of the virtual file
	``/proc/cpuinfo''. This file contains information about the processor(s) used
	in the system. Many of these information are useful in order to do a
	\emph{fine tuning} on the kernel. See Listing \ref{lst:cpuinfo} for an example
	output of this command.}

\paperDescription{lspci}{This command lists all PCI devices connected in the system. This
	is useful to deactivate unnecessary hardware driver on the kernel
	configuration and therefore optimize the kernel. See Listing \ref{lst:lspci}
	for an example output of this command.}

As there are many more changes necessary to get an optimal kernel for a
target hardware device, there must be also many more methods invoked to be able
to gather all essential information. Different books cover the
topic ``kernel optimization'', therefore this paper will not go that far.
Building an optimal kernel for a specific hardware is an evolving process,
because there are changing requirements due to fast growing and
agile information technology. 

K. Lowe provides a \emph{Kernel Rebuild Guide} if more
information on this topic is needed \cite{url:kernelconfig}.

\begin{lstlisting}[label=lst:cpuinfo, float=!t, language=bash,
caption=Running 'cat /proc/cpuinfo']
# Run the command
> cat /proc/cpuinfo
# We have two times the following output
# (For each core one output, only the processor id is changing)
processor		: 0
vendor_id		: GenuineIntel
cpu family		: 6
model			: 15
model name		: Intel(R) Core(TM)2 CPU U7500  @ 1.06GHz
stepping		: 2
cpu MHz			: 800.000
cache size		: 2048 KB
physical id		: 0
siblings		: 2
core id			: 0
cpu cores		: 2
apicid			: 0
initial apicid	: 0
fdiv_bug		: no
hlt_bug			: no
f00f_bug		: no
coma_bug		: no
fpu				: yes
fpu_exception	: yes
cpuid level		: 10
wp				: yes
flags			: fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36
clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc
arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr
pdcm lahf_lm tpr_shadow
bogomips		: 2127.88 
clflush size	: 64 
power management:
\end{lstlisting}

\begin{lstlisting}[label=lst:lspci, float=!t, language=bash,
caption=Running 'lspci']
# Run the command
> lspci
00:00.0 Host bridge: Intel Corporation Mobile 945GM/PM/GMS, 943/940GML and 945GT Express Memory Controller Hub (rev 03)
00:02.0 VGA compatible controller: Intel Corporation Mobile 945GM/GMS, 943/940GML Express Integrated Graphics Controller (rev 03)
00:02.1 Display controller: Intel Corporation Mobile 945GM/GMS/GME, 943/940GML Express Integrated Graphics Controller (rev 03)
00:1b.0 Audio device: Intel Corporation 82801G (ICH7 Family) High Definition Audio Controller (rev 02)
00:1c.0 PCI bridge: Intel Corporation 82801G (ICH7 Family) PCI Express Port 1 (rev 02)
... # Some parts left out
02:00.0 Ethernet controller: Marvell Technology Group Ltd. 88E8055 PCI-E Gigabit Ethernet Controller (rev 13)
03:00.0 Network controller: Intel Corporation PRO/Wireless 3945ABG [Golan] Network Connection (rev 02)
09:04.0 CardBus bridge: Ricoh Co Ltd RL5c476 II (rev ba)
09:04.1 FireWire (IEEE 1394): Ricoh Co Ltd R5C832 IEEE 1394 Controller (rev 04)
09:04.3 System peripheral: Ricoh Co Ltd R5C843 MMC Host Controller (rev ff)
09:04.4 System peripheral: Ricoh Co Ltd R5C592 Memory Stick Bus Host Adapter (rev 11)
\end{lstlisting}

\subsection{Using a Ubuntu kernel}
\label{subsec:ubuntuKern}
As this Linux distribution is one of the widest spread and most often used
distribution in this time, the Ubuntu team also developed an kernel
with real-time capabilities. This kernel can be installed in a simple way
through the systems packet manager. Listing \ref{lst:aptRTKernel} illustrates
how to install a real-time kernel with the Ubuntu package sources. The fact that
all Ubuntu kernel patches are already applied is an advantage. Furthermore
this kernel provides all the functionalities based on an Ubuntu kernel. In
addition to that, this kernel is extensively tested by experienced users and
developers, which leads to an advanced evolving state. In the following tests,
this kernel is used as reference.

\begin{lstlisting}[label=lst:aptRTKernel, float=!t, language=bash,
caption=Installing Ubuntu-RT Kernel] 
# Install the kernel through the packet manager
> sudo apt-get install linux-rt
\end{lstlisting}